package com.system.vote.admin.controller.app;

import com.system.vote.admin.security.JwtUtil;
import com.system.vote.admin.service.TVoteService;
import com.system.vote.core.http.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/app/vote/statistics")
@Api(tags = "App统计管理")
public class AppStatisticsController {


    @Autowired
    private TVoteService tVoteService;

    @GetMapping("/statisticsVote")
    public Result statisticsVote(HttpServletRequest request) {
        String token=request.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        String uid = claims.getId();
        int voting=tVoteService.statisticsVoting(Long.valueOf(uid));
        int endVote=tVoteService.statisticsEndVote(Long.valueOf(uid));
        int voted=tVoteService.statisticsVoted(Long.valueOf(uid));
        List<Map<String, Object>> list=new ArrayList<>();

        Map<String, Object> votingObj = new HashMap<>();
        votingObj.put("value",voting);
        votingObj.put("name","进行中");
        list.add(votingObj);

        Map<String, Object> endVoteObj = new HashMap<>();
        endVoteObj.put("value",endVote);
        endVoteObj.put("name","已结束");
        list.add(endVoteObj);

        Map<String, Object> votedObj = new HashMap<>();
        votedObj.put("value",voted);
        votedObj.put("name","已投票");
        list.add(votedObj);

        return Result.success(list);
    }
}
